<template>
	<div class="u-page" style="background-color: #fdf3f6;">
		<!-- <u-navbar :is-back="false" :title="title" :background="background" :border-bottom="false" :title-bold="true" title-color="#040503"></u-navbar> -->
		<u-navbar :is-back="false" :background="background" :border-bottom="false">
		  <view class="slot-wrap">
		    <u-tabs :list="Navbar.list" :is-scroll="false" inactive-color="#a2a0a1" active-color="#eb5468"
		            bg-color="#f5d7e2" bar-height="10"
		            :active-item-style="Navbar.bar" item-width="190" bar-width="180"></u-tabs>
		  </view>
		</u-navbar>
		<div style="background: linear-gradient(to bottom, #f5d7e2, #fdf3f6);">
			<div class="u-page u-box">
				<!-- 用户信息 -->
				<div class="u-flex space-between u-cl-white u-mt-10">
					<div class="u-flex">
						<div class="u-mr-10">
							<u-avatar :src="UserInfo.avatarUrl" size="120"></u-avatar>
						</div>
						<div class="u-flex-1 u-pl-5 color-black">
							<div class="u-flex">
								<div class="u-title u-pointer">
									{{UserInfo.nickName}}
								</div>
							</div>
							<div class="fw-545" @click="copyBtn(UserInfo.userId)">ID：{{UserInfo.userId}}<span class="u-pl-5" @click="copyBtn(UserInfo.userId)">复制</span></div>
						</div>
					</div>
					<div @click="editUserInfo">
						<u-icon name="setting" color="#000000" size="45"></u-icon>
					</div>
				</div>
			</div>

			<div class="u-mt-25 application-bar">
				<div class="application-bar-item" v-for="(item,index) in ApplicationBar" :key="index" @click="toApplicationBar(item)">
					<u-image width="90rpx" height="90rpx" :src="item.image"></u-image>
					<div class="u-pt-5">{{item.title?item.title:''}}</div>
				</div>
			</div>

			<div class="u-pt-10">
				<div class="provider-item">
					<div style="width: 17%;"></div>
					<div class="provider-item-center">
						<div>
							<div class="provider-center-title u-fz-15 u-font-bold">
								<span v-if="Provider.providerId">区域服务商 NO.{{Provider.providerId}}</span>
								<span v-else>招募区域服务商</span>
							</div>
							<div class="u-fz-11 u-pt-5" style="color: #d6d6d6;" v-if="Provider.providerId">
								<span>{{Provider.area}}</span>
							</div>

							<div class="u-fz-9 u-pt-5" style="color: #d6d6d6;" v-else>
								<span>可获得工作报酬、团购返利、区域奖励等最大化收益。</span>
							</div>

						</div>
					</div>
					<div class="provider-item-right">
						<div class="provider-item-right-btn">
							<span @click="toEvaluatorInfo" v-if="Provider.providerId">查看资料</span>
							<span @click="applyServiceProviderShow" v-else>立即申请</span>
						</div>
					</div>
				</div>
			</div>
		</div>

		<div class="module-box">
			<div class="module-box-title">
				个人收益
			</div>
			<div class="income-box">
				<div class="u-pt-15 income-money">
					<div class="u-center">
						<span class="fz-30 u-font-bold">￥{{MyWealth.Money?MyWealth.Money:0}}</span>
					</div>
					<div class="income-money-text" @click="toBalanceDetails">余额明细<span style="vertical-align: text-top;padding-left: 3rpx;"><u-icon name="arrow-right" size="28" color="#403d3e"></u-icon></span></div>
				</div>
				<div class="pane u-pt-10" style="display:block;">
					<div class="dp-number-current clearfix">
						<ol>
							<li :class="item.isSelected?'current':''" v-for="(item,index) in OptionalRMB" :key="index" @click="selectedCore(item)">{{item.rmb}}元</li>
						</ol>
					</div>
				</div>
				<div class="u-pt-10 cash-out-btn">
					<u-button :custom-style="customStyleCash" type="primary" shape="circle" size="mini" hover-class="none" @click="isCanCashOut">
						<span class="u-pr-5" v-if="CashOutAmount != ''">￥{{CashOutAmount}}</span>立即提现
					</u-button>
				</div>
				<div class="u-pt-15 u-pb-15 income-tips">
					<p>
						受微信小程序提现限制，每天提现最高限额200元且最多提现5笔
					</p>
					<p>
						如需大额提现或提现出错，请联系<span class="u-font-bold" @click="openCustomerServiceChat">工作人员</span>
					</p>
				</div>
			</div>
		</div>

		<div class="module-box height300">
			<div class="module-box-title">
				我的权益
			</div>
			<div class="u-flex u-pt-20">
				<div class="functions-item" v-for="(item,index) in FunctionPoints" :key="index" @click="executeAction(item)">
					<u-image width="50rpx" height="50rpx" :src="item.image"></u-image>
					<div style="padding-top: 5rpx;">{{item.name}}</div>
					<div style="font-size: 24rpx;color: #bfbebe;">
						{{item.numPrefix}}
						<span style="color: #f33b4f;">{{item.num}}</span>
						{{item.numSuffix}}
					</div>
				</div>
			</div>
		</div>

		<div class="module-box height270" v-if="Authority.canStartBusiness">
			<div class="module-box-title">
				我的应用
			</div>
			<div class="u-flex u-pt-20">
				<div class="applications-item" v-for="(item,index) in Applications" :key="index" @click="executeAction(item)">
					<u-image width="50rpx" height="50rpx" :src="item.image"></u-image>
					<div style="padding-top: 5rpx;">{{item.name}}</div>
				</div>
			</div>
		</div>

		<div class="u-pb-10" style="width: 95%;margin: 0 auto;" v-if="AdsList && AdsList.length > 0">
			<u-swiper :list="AdsList" height="200" border-radius="20" @click="clickSwiper"></u-swiper>
		</div>

		<!-- 完善用户信息：昵称和头像 -->
		<div>
			<u-modal
			  ref="uModal"
			  :show-title="false"
			  v-model="ShowEditUserInfo"
			  :mask-close-able="true"
			  :show-confirm-button="false"
			>
				<div class="u-page u-general-color">
					<div class="u-center user-head-bg" style="padding-top: 130rpx;">
						<button class="aui-flex-user" open-type="chooseAvatar" @chooseavatar="onChooseAvatar">
							<img :src="temp_avatarUrl" alt="">
							<span class="aui-flex-vip">
								<u-icon name="camera-fill" color="#eb5468" size="40"></u-icon>
							</span>
						</button>
					</div>
					<div class="u-mt-20 u-ml-20 u-mr-20" style="border-radius: 15rpx;">
						<div class="u-flex" style="padding: 15rpx 20rpx;background: #fff;">
							<div class="u-pr-5 u-fz-15" style="color: #868585;">昵称</div>
							<div style="width: 85%;">
								<u-input v-model="temp_nickName" type="nickname" :custom-style="inputStyle" />
							</div>
						</div>
					</div>
					<div class="u-mt-30 u-pl-20 u-pr-20">
						<div style="display: flex;justify-content: space-between;">
							<div class="" style="width: 100rpx;height: 100rpx;border-radius: 10rpx;">
								<div style="line-height: 0rpx;padding-top: 27rpx;padding-left: 27rpx;">
									<u-icon name="weixin-fill" color="#4dc822" size="50"></u-icon>
								</div>
							</div>
							<div style="width: 78%;font-size: 24rpx;">
								为保障您在平台能够顺利参与工作，赚取劳动报酬，我们建议您使用与当前微信一致的头像和昵称！
							</div>
						</div>
					</div>
					<div class="u-pt-30 u-pb-20 u-pl-20 u-pr-20">
						<u-button type="primary" :custom-style="customStyle" @click="ConfirmEditUserInfo">确定使用</u-button>
					</div>
				</div>
			</u-modal>
		</div>

		<!-- 申请区域服务商弹窗 -->
		<u-modal v-model="ApplyServiceProvider_show" confirm-text="已知晓" @confirm="toApplyServiceProvider" title="区域服务商申请和权益说明" confirm-color="#eb5468" :mask-close-able="true">
			<div class="slot-content2">
				<scroll-view scroll-y="true" style="height: 800rpx;">
					<div class="u-title">· 资格申请及收益：</div>
					<p>1、所有用户自动赋予线上服务商资格。</p>
					<p>2、用户须提交申请，方可获得区域服务商资格。</p>
					<p>3、按照“省-市-县区-街道-社区”的5级分类，确定区域归属。</p>
					<p>4、区域服务商可获得工作报酬，团购返利，区域奖励等最大化收益。</p>
					<div class="u-title u-pt-5">· 玫丽责任及义务：</div>
					<p>5、玫丽提供优质品牌方的推广、销售、服务等工作机会。</p>
					<p>6、工作机会创造的工作价值，将按照区域归属和业绩占比等条件，自动分配。</p>
					<div class="u-title u-pt-5">· 区域服务商责任及义务：</div>
					<p>7、积极参与平台发布的工作机会，传播及引导区域内的用户参与，共同创造工作价值。</p>
					<div class="u-title u-pt-5">· 区域服务商资格取消及升级：</div>
					<p>8、区域服务商，每申请一次，授权期限为6个月。逾期后，需重新申请。</p>
					<p>9、区域服务商可申请成为“街道”级别的唯一服务商，详情请联系客服。</p>
					<p>10、区域服务商可申请成为“县区级”级别的业务合伙人，详情请联系客服。</p>
					<p>11、已有“县区级”业务合伙人的区域，不再设立“街道”唯一服务商。</p>
					<p>12、“街道”唯一服务商和“县区级”业务合伙人，需达到相应工作指标。不达标者，平台取消授权。</p>
				</scroll-view>
			</div>
		</u-modal>

		<!-- 客服弹窗 -->
		<div>
		  <u-modal v-model="KF_show" title="客服微信" confirm-color="#e55e97">
		    <view class="slot-content">
			  <img :src="customerServiceUrl" @longpress="saveImg"></img>
		    </view>
		  </u-modal>
		</div>

		<!-- 提现成功弹窗 -->
		<div>
		  <u-modal v-model="TX_show" :content="TX_content" title="提示" confirm-color="#e55e97"></u-modal>
		</div>

		<!-- 品牌推广 -->
		<u-popup v-model="branding_show" mode="center" width="400rpx" height="400rpx" border-radius="10">
			<div style="width: 100%;height: 400rpx;display: flex;flex-direction: column;justify-content: center;align-items: center;">
				<div>
					<u-button type="error" plain @click="$u.route('pages_tabbar_contact/personal_contact/SelectProductList')">发起新的活动</u-button>
				</div>
				<div class="u-pt-15">
					<u-button type="info" @click="$u.route('pages_tabbar_contact/personal_contact/MyEvaluations')">查看历史活动</u-button>
				</div>
			</div>
		</u-popup>
		<!-- 样品推广 -->
		<u-popup v-model="samplePromotion_show" mode="center" width="400rpx" height="400rpx" border-radius="10">
			<div style="width: 100%;height: 400rpx;display: flex;flex-direction: column;justify-content: center;align-items: center;">
				<div>
					<u-button type="error" plain @click="$u.route('pages_tabbar_contact/personal_contact/SelectProductList?tag=sample')">发起新的活动</u-button>
				</div>
				<div class="u-pt-15">
					<u-button type="info" @click="$u.route('pages_tabbar_contact/personal_contact/ProvideSamplesList')">查看历史活动</u-button>
				</div>
			</div>
		</u-popup>

		<!-- 收益弹窗提醒 -->
		<u-modal v-model="income_show" confirm-text="前往查询" @confirm="toBalanceDetails" title="收益提醒" 
			confirm-color="#eb5468" :mask-close-able="true" width="500">
			<div class="slot-content3">
				叮～您有新的收益已到账！
			</div>
		</u-modal>

		<!-- 公告 -->
		<Notice ref="noticeModule"></Notice>
	</div>
</template>

<script>
import globalData from "@/globalData";
import COS from "@/common/js/cos-wx-sdk-v5.min.js";
import navbar from "../components/navbar.vue"

export default {
  components:{
  	  navbar
  },

  data() {
    return {
	  SceneAssetsId: '',
	  UserInfo: {},
	  url: globalData.inviteURL,
	  levelURL: globalData.iconURL + "PersonalIndex/LevelIcon.png",
	  IntegralURL: globalData.iconURL + "PersonalIndex/Integral.png",
	  CertifiedPartnerURL: globalData.ICON.personalIndex5,
	  Navbar: {
	    list: [
	      {
	        name: '我的收益'
	      }
	    ],
	    bar: {
	      color: '#34292d',
	      fontSize: '46rpx'
	    },
	    background: {
	      background: '#f5d7e2'
	    }
	  },
	  // title: '我的收益',
	  background: {
	  	// backgroundImage: 'linear-gradient(to right , rgb(244,227,233), rgb(246,230,236))'
		backgroundColor: '#f5d7e2'
	  },
	  ShowEditUserInfo: false,//弹窗修改用户信息标志
	  temp_nickName: '',//弹窗修改用户头像和昵称的临时昵称
	  temp_avatarUrl: '',//弹窗修改用户头像和昵称的临时头像
	  delAvatarUrl: [],//确定更改头像之后需要删除的头像列表
	  Authority: {
		  canStartBusiness: false//是否可以发起业务
	  },
	  customStyle: {
	  	background: '#eb5468 !important'
	  },
	  inputStyle: {
		  background: "#fff"
	  },
	  ApplyServiceProvider_show: false,//申请区域服务商弹窗
	  Provider: {},//区域服务商信息
	  isNewSeaSpace: false,//是否海上新空间会员
	  ApplicationBar: [//应用栏
		  {
			  image: 'https://www-1308272144.cos.ap-guangzhou.myqcloud.com/Meily/img/IncomeIndex/MyTodolist.png',
			  title: '我的待办',
			  url: '/pages_tabbar_contact/personal_contact/WorkToDo'
		  },
		 //  {
			// image: 'https://www-1308272144.cos.ap-guangzhou.myqcloud.com/Meily/img/Index/initiateCollaboration.png',
			// title: '协作待办',
			// url: '/pages_tabbar_contact/personal_contact/WorkToDo2'
		 //  },
		  {
			  image: 'https://www-1308272144.cos.ap-guangzhou.myqcloud.com/Meily/img/IncomeIndex/MyOders.png',
			  title: '我的订单',
			  url: '/pages_private_scene2/GroupPurchase/OrderList'
		  },
		  {
			  image: 'https://www-1308272144.cos.ap-guangzhou.myqcloud.com/Meily/img/IncomeIndex/MyAgent.png',
			  title: '我的代理',
			  url: '/pages_tabbar_contact/personal_contact/SolitaireList'
		  },
		  {
			  image: 'https://www-1308272144.cos.ap-guangzhou.myqcloud.com/Meily/img/IncomeIndex/MessageCenter.png',
			  title: '消息中心',
			  url: '/pages_tabbar_contact/personal_contact/MyMessages'
		  }
	  ],
	  MyWealth: {
	  		  Integral: 0,
	  		  Money: 0
	  },
	  OptionalRMB: [
	  		  {
	  			  rmb: 1,
	  			  isSelected: false
	  		  },
	  		  {
	  			  rmb: 2,
	  			  isSelected: false
	  		  },
	  		  {
	  			  rmb: 5,
	  			  isSelected: false
	  		  },
	  		  {
	  			  rmb: 10,
	  			  isSelected: false
	  		  },
	  		  {
	  			  rmb: 20,
	  			  isSelected: false
	  		  },
	  		  {
	  			  rmb: 50,
	  			  isSelected: false
	  		  },
	  		  {
	  			  rmb: 100,
	  			  isSelected: false
	  		  },
	  		  {
	  			  rmb: 200,
	  			  isSelected: false
	  		  }
	  ],
	  customflag: false,
	  CashOutAmount: '',
	  CustomAmount: '',
	  customStyleCash: {
		  backgroundColor: '#eb5468',
		  width: '400rpx',
		  height: '70rpx',
		  lineHeight: '70rpx',
		  fontSize: '30rpx'
	  },
	  FunctionPoints: [
		  {
			  image: 'https://www-1308272144.cos.ap-guangzhou.myqcloud.com/Meily/img/IncomeIndex/Coupon.png',
			  name: '优惠券',
			  num: '0',
			  numPrefix: '共',
			  numSuffix: '张',
			  action: 1,
			  url: '/pages_tabbar_contact/personal_contact/MyCoupons'
		  },
		  {
			  image: 'https://www-1308272144.cos.ap-guangzhou.myqcloud.com/Meily/img/IncomeIndex/ImproveInfo.png',
			  name: '完善资料',
			  num: '',
			  numPrefix: '参与工作',
			  numSuffix: '',
			  action: 1,
			  url: '/pages_tabbar_contact/personal_contact/CustomerInfo'
		  },
		  {
			  image: 'https://www-1308272144.cos.ap-guangzhou.myqcloud.com/Meily/img/IncomeIndex/InvitationPolite.png',
			  name: '邀请有礼',
			  num: '10',
			  numPrefix: '',
			  numSuffix: '元奖励',
			  action: 0
		  },
		  {
			  image: 'https://www-1308272144.cos.ap-guangzhou.myqcloud.com/Meily/img/IncomeIndex/GroupWelfare.png',
			  name: '进群福利',
			  num: '5-100',
			  numPrefix: '',
			  numSuffix: '元报酬',
			  action: 2
		  }
	  ],
	  Applications: [
		  {
			  image: 'https://www-1308272144.cos.ap-guangzhou.myqcloud.com/Meily/img/IncomeIndex/Branding.png',
			  name: '品牌推广',
			  action: 3,
			  url: 'branding_show'
		  },
		  {
			  image: 'https://www-1308272144.cos.ap-guangzhou.myqcloud.com/Meily/img/IncomeIndex/SamplePromotion.png',
			  name: '样品推广',
			  action: 3,
			  url: 'samplePromotion_show'
		  }
	  ],
	  AdsList: [],
	  KF_show: false,//客服弹窗
	  customerServiceUrl: globalData.customerService,//客服微信二维码图片地址
	  TX_show: false,//提现成功弹窗
	  TX_content: '提现成功，预计1分钟内转入微信钱包',//提现成功弹窗显示内容
	  branding_show: false,//品牌推广弹窗
	  samplePromotion_show: false,//样品推广弹窗
	  income_show: false//新收益提醒弹窗
    };
  },

  onLoad() {
    //未登录跳转到登录页面
    var loginRes = this.checkLogin();
    if (loginRes) {
      this.UserInfo = uni.getStorageSync('userInfo')
	  this.initMyWealth()
	  this.getAds()
	  this.canStartBusiness()
	  this.getUserCouponsCount()//加载优惠券数量
    }
  },

  onShow(){
	  this.getProviderInfo()
	  this.NewSeaSpace()
	  this.reloadNotice()
	  this.incomeRemind()//新收益提醒
  },

  onPullDownRefresh() {
	this.initMyWealth()
	this.getUserCouponsCount()//加载优惠券数量
	this.getAds()
	// 停止Loading
	setTimeout(() => {
		// 停止Loading
		uni.stopPullDownRefresh();
	}, 1500)
  },

  methods: {

	/**
	 * 获取是否能发起业务的权限
	 */
	async canStartBusiness() {
		let result = await this.$u.api.unifyMiniRestGet({
			systemid: "meily",
			url: "/role/query/start-business/"+this.UserInfo.userId,
		    loading: false // 默认发起请求会有一个全局的Loading，设置false可去掉
		});
		if(result.code == 200){
			this.Authority.canStartBusiness = result.data
		}
	},

	//点击一键复制
	copyBtn (text) {
		wx.setClipboardData({
		  //准备复制的数据内容
		  data: text,
		  success: function (res) {
			wx.showToast({
			  title: '已复制',
			});
		  }
		});
	},

	/**
	 * 此功能暂未开放
	 */
	notOpenTips() {
		this.$u.toast("此功能暂未开放")
	},

	/**
	 * 弹出修改用户昵称和头像弹窗
	 */
	editUserInfo() {
		this.temp_avatarUrl = this.UserInfo.avatarUrl
		this.temp_nickName = this.UserInfo.nickName
		this.ShowEditUserInfo = true
	},

	/**
	 * 头像上传
	 */
	onChooseAvatar(e){
		let _this = this
		// SECRETID 和 SECRETKEY请登录 https://console.cloud.tencent.com/cam/capi 进行查看和管理
		const Cos = new COS({
			SecretId: globalData.CosSecretId,
			SecretKey: globalData.CosSecretKey,
		});
		//解析原头像地址的随机值，重新生成随机值的时候排除原地址随机值，避免地址相同导致虽然图片已经上传但前端检测不到地址变化不更新头像
		// let ramdom_sault = _this.getRamdomSault(_this.temp_avatarUrl,_this.UserInfo.avatarUrl)
		let folderName = "perpartner/UserAvatar/";
		let upload_fileName = _this.UserInfo.userId + '_' + _this.guid() + '.png'
		Cos.postObject({
		    Bucket: globalData.CosBucket,
		    Region: globalData.CosRegion,
		    Key: folderName + upload_fileName,// 文件夹+文件名称
		    FilePath: e.detail.avatarUrl,
		    onProgress: function (info) {
		        console.log(JSON.stringify(info));
		    }
		}, function (err, data) {
		    console.log(err || data);
			if(data){
				// data.Location返回的路径，会转成%之类的，导致不能直接访问，所以路径需要自己拼
				// let backUrl = "https://" + data.Location;
				let backUrl = "https://" + globalData.CosBucket + ".cos."+ globalData.CosRegion + ".myqcloud.com/" + folderName + upload_fileName;
				_this.temp_avatarUrl = backUrl
				//删除cos存的原来的头像
				// let cos_key = _this.getKeyByAvatar(_this.UserInfo.avatarUrl)
				// Cos.deleteObject({
				//     Bucket: globalData.CosBucket,
				//     Region: globalData.CosRegion,
				//     Key: cos_key,  // 存储在桶里的对象键（例如1.jpg，a/b/test.txt），必须字段
				// }, function(err2, data2) {
				//     console.log(err2 || data2);
				// 	if(data2) {
				// 		_this.showMSG("none","删除原头像成功")
				// 	}else {
				// 		_this.showMSG("none","删除原头像失败")
				// 	}
				// });
			}else{
				_this.showMSG('none','头像上传失败，请重新上传')
			}
		});
	},

	/**
	 * 确定完善用户信息
	 */
	async ConfirmEditUserInfo(){
		let _this = this
		if(_this.isNotNull(_this.temp_nickName) && _this.isNotNull(_this.temp_avatarUrl)){
			_this.ShowEditUserInfo = false
			//请求修改用户头像和昵称的接口
			let result = await _this.$u.api.unifyMiniRestGet({
				systemid: "sso", //必须，系统名称，这里不同的系统对接请求地址的域名前串不同，各请求地址统一写在一个全局配置文件中
				url: "/system/user/profile/"+_this.UserInfo.userId+"/meily", //必须，接口请求地址
				loading: true
			});
			if(result.code == 200) {
				let user_info = result.data
				let res2 = await _this.$u.api.unifyMiniRest({
					systemid: "sso", //必须，系统名称，这里不同的系统对接请求地址的域名前串不同，各请求地址统一写在一个全局配置文件中
					url: "/system/user/profile/update" ,//必须，接口请求地址
					appletId: "meily",
					userid: this.UserInfo.userId,
					unionid: this.UserInfo.unionid,
					nickname: _this.temp_nickName,
					avatar: _this.temp_avatarUrl,
					loading: true
				});
				if(res2.code == 200) {//更新成功，将新的用户信息set进缓存
					_this.UserInfo.nickName = _this.temp_nickName
					_this.UserInfo.avatarUrl = _this.temp_avatarUrl
					_this.SET_USER_INFO(_this.UserInfo)
					_this.showMSG('success','保存成功')
				}else {
					_this.showMSG('error','出错啦')
				}
			}else {
				_this.showMSG('error','出错啦')
			}
		}else{
			_this.showMSG('none','须上传头像和填写昵称才能修改噢')
		}

	},

	/**
	 * 解析原头像地址的随机值，重新生成随机值的时候排除原地址随机值，避免地址相同
	 */
	getRamdomSault(avatar_url,original_url){
		let sault = 0
		let original_sault = ''
		if(this.isNotNull(original_url)) {
			let original_flag = original_url.substring(original_url.indexOf('.png')-2,original_url.indexOf('.png')-1)
			if(original_flag == "_") original_sault = original_url.substring(original_url.indexOf('.png')-1,original_url.indexOf('.png'))
		}
		if(this.isNotNull(avatar_url)){
			//查找.png前两个字符是否为"_"
			let flag = avatar_url.substring(avatar_url.indexOf('.png')-2,avatar_url.indexOf('.png')-1)
			if(flag == "_"){
				sault = avatar_url.substring(avatar_url.indexOf('.png')-1,avatar_url.indexOf('.png'))
				if(sault == 0){
					if(original_sault != '' && original_sault == 1){
						sault = 2
					}else{
						sault = 1
					}
				}else if(sault == 1){
					if(original_sault != '' && original_sault == 0){
						sault = 2
					}else{
						sault = 0
					}
				}else if(sault == 2){
					if(original_sault != '' && original_sault == 0){
						sault = 1
					}else{
						sault = 0
					}
				}
			}
		}
		return sault
	},

	/**
	 * 根据头像地址获取cos的key
	 */
	getKeyByAvatar(avatar_url) {
		return avatar_url.substring(avatar_url.indexOf('perpartner'),avatar_url.length-1)
	},

	/**
	 * 获取区域服务商信息：用于判断当前用户是否已经申请成为了区域服务商
	 */
	async getProviderInfo() {
		let result = await this.$u.api.unifyMiniRest({
			systemid: "meily",
			url: "/provider/info",
			userId: this.UserInfo.userId,
		    loading: false // 默认发起请求会有一个全局的Loading，设置false可去掉
		});
		if(result.code == 200){
			this.Provider = result.data
		}
	},

	/**
	 * 申请区域服务商弹窗
	 */
	async applyServiceProviderShow() {
		//校验是否已经注册过了
		let result = await this.$u.api.unifyMiniRest({
			systemid: "meily",
			url: "/provider/info",
			userId: this.UserInfo.userId,
		    loading: false // 默认发起请求会有一个全局的Loading，设置false可去掉
		});
		if(result.code == 200 && this.isNotNull(result.data)){
			this.Provider = result.data
		}else{
			this.ApplyServiceProvider_show = true
		}
	},

	/**
	 * 跳转到申请区域服务商页面
	 */
	toApplyServiceProvider() {
		this.$u.route('/pages_tabbar_contact/personal_contact/VPSRegister')
	},

	/**
	 * 跳转到个人资料
	 */
	toEvaluatorInfo(){
		this.$u.route('/pages_tabbar_contact/personal_contact/CustomerInfo')
	},


	/**
	 * 判断当前用户是否为海上新空间会员
	 */
	async NewSeaSpace() {
		let result = await this.$u.api.unifyMiniRest({
			systemid: "meily",
			url: "/space/accept/checkUser",
			userId: uni.getStorageSync('userInfo').userId,
			loading: true // 默认发起请求会有一个全局的Loading，设置false可去掉
		});
		if (result.code == 200) {
			this.isNewSeaSpace = true
		}
	},

	/**
	 * 获取广告图
	 */
	async getAds() {
		let result = await this.$u.api.unifyMiniRest({
			systemid: "meily",
			url: "/config/list",
			configType: "income"
		});
		if(result.code == 200){
			let rows = result.rows
			if(rows.length > 0){
				this.AdsList = []
				for(const i in rows){
					if(rows[i].configKey == 'income-job'){
						let obj = {image: rows[i].imageLink.split(",")[0],link: rows[i].configLink,source: rows[i].configFrom}
						this.AdsList.push(obj)
					}
				}
			}
		}
	},

	/**
	 * 点击系统基础功能栏
	 */
	toApplicationBar(item) {
		if(!this.$u.test.isEmpty(item.url)){
			this.$u.route(item.url)
		}
	},

	/**
	 * 选择兑换金额
	 */
	selectedCore(item){
		for(let i in this.OptionalRMB){
			this.OptionalRMB[i].isSelected = false
		}
		item.isSelected = true
		this.CashOutAmount = item.rmb
		this.customflag = false
	},

	/**
	 * 跳转到余额明细页面
	 */
	toBalanceDetails() {
		this.$u.route('/pages_tabbar_contact/income_contact/balanceDetails')
	},

	/**
	 * 点击联系客服
	 */
	async openCustomerServiceChat() {
		let result = await this.$u.api.unifyMiniRestGet({
			systemid: "meily",
			url: "/qyapi/kf/account/url/".concat(globalData.openKfid_platform),
			loading: true // 默认发起请求会有一个全局的Loading，设置false可去掉
		});
		if (result.code == 200) {
			const pages = getCurrentPages(); // 获取栈实例
			//当前页面路径(带参数)
			let currentPage = pages[pages.length - 1]['$page']['fullPath']
			let kfUrl = result.data
			wx.openCustomerServiceChat({
				extInfo: {
					url: kfUrl
				},
				corpId: globalData.corpId,
				showMessageCard: true,
				sendMessagePath: currentPage,
				success(res) {
					console.log(res)
				}
			})
		} else {
			this.showMSG('error', '出错啦！');
		}
	},

	/**
	 * 点击功能块
	 */
	async executeAction(item) {
		if(item.action == 0) {
			this.showMSG('none','关注我们，敬请期待！')
		}else if(item.action == 1) {
			this.$u.route(item.url)
		}else if(item.action == 2) {
			this.openWelfareServiceChat()
		}else if(item.action == 3){
			if(item.url == 'branding_show'){
				this.branding_show = true
			}else if(item.url == 'samplePromotion_show'){
				this.samplePromotion_show = true
			}
		}
	},

	/**
	 * 联系客服
	 */
	async openWelfareServiceChat() {
		let result = await this.$u.api.unifyMiniRestGet({
		   systemid: "meily",
		   url: "/qyapi/kf/account/url/".concat(globalData.openKfid_welfare),
		   loading: true // 默认发起请求会有一个全局的Loading，设置false可去掉
		});
		if (result.code == 200) {
		   const pages = getCurrentPages(); // 获取栈实例
		   //当前页面路径(带参数)
		   let currentPage = pages[pages.length - 1]['$page']['fullPath']
		   let kfUrl = result.data
		   wx.openCustomerServiceChat({
			extInfo: {
			 url: kfUrl
			},
			corpId: globalData.corpId,
			showMessageCard: true,
			sendMessagePath: currentPage,
			success(res) {
			 console.log(res)
			}
		   })
		} else {
		   this.showMSG('error', '出错啦！');
		}
	},

	/**
	 * 获取优惠券数据
	 */
	async getUserCouponsCount(){
		let params = {
			userId: this.UserInfo.userId,
		}
		let token = await this.getToken(params);
		let tmpToken = this.sault_md5_token(token);
		params.systemid = "payment";
		params.token = token;
		params.authStr = tmpToken;
		params.url = "/coupon/user/count";
		let result = await this.$u.api.unifyMiniRest(params);
		if (result.code == 200) {
			if(result[0]){
				this.FunctionPoints[0].num = result[0]
			}
		}
	},
	async getToken(params) {
		params.systemid = "payment";
		params.url = "/points/token";
		let result = await this.$u.api.unifyMiniRest(params);
		let token = "";
		if (result.code == 200) {
	    token = result.data
		}
		return token;
	},

	/**
	 * 查询可用余额
	 */
	async initMyWealth() {
		let res = await this.$u.api.unifyMiniRest({
			systemid: "payment",
			url: "/points/token",
			userId: this.UserInfo.userId,
			loading: false
		});
		if(res.code == 200 && this.isNotNull(res.data)){
			let token = res.data
			let tmpToken = this.sault_md5_token(token)
			let result = await this.$u.api.unifyMiniRest({
				systemid: "payment",
				url: "/wallet/getCoins",
				userId: this.UserInfo.userId,
				token: token,
				authStr: tmpToken,
				loading: false
			});
			if(result.code == 200){
				this.MyWealth.Money = result.data
			}
		}else{
			this.showMSG('none','获取token出错啦')
		}
	},

	/**
	 * 判断用户是否可以提现
	 */
	async isCanCashOut() {
		let result = await this.$u.api.unifyMiniRestGet({
			systemid: "meily",
			url: "/user/canCashOut/"+uni.getStorageSync('userInfo').userId,
		});
		if(result.code == 200){
			if(result.data){
				//发起提现
				this.CashOut()
			}else{
				this.showMSG('none','提现功能受限，请联系客服')
			}
		}else{
			this.showMSG('error','出错啦')
		}
	},

	/**
	 * 发起提现
	 */
	async CashOut(){
		//是否需要弹出客服二维码
		let flag = false
		//再次调用获取查询余额接口验证可用余额
		let res = await this.$u.api.unifyMiniRest({
			systemid: "payment",
			url: "/points/token",
			userId: this.UserInfo.userId,
			loading: false
		});
		if(res.code == 200 && this.isNotNull(res.data)){
			let token = res.data
			let tmpToken = this.sault_md5_token(token)
			let result = await this.$u.api.unifyMiniRest({
				systemid: "payment",
				url: "/wallet/getCoins",
				userId: this.UserInfo.userId,
				token: token,
				authStr: tmpToken,
				loading: true
			});
			if(result.code == 200){
				this.MyWealth.Money = result.data
				if(parseFloat(this.MyWealth.Money) < parseFloat(this.CashOutAmount) || !this.isNotNull(this.CashOutAmount)){//可用余额是否大于等于提现金额
					if(!this.isNotNull(this.CashOutAmount)){
						this.showMSG('none','请选择提现金额')
					}else{
						this.showMSG('none','提现金额不能超过可用余额噢!')
					}
				}else{
					let res2 = await this.$u.api.unifyMiniRest({
						systemid: "payment",
						url: "/points/token",
						userId: this.UserInfo.userId,
						loading: false
					});
					if(res2.code == 200 && this.isNotNull(res2.data)){
						let token2 = res2.data
						let tmpToken2 = this.sault_md5_token(token2)
						let result2 = await this.$u.api.unifyMiniRest({
							systemid: "payment",
							url: '/wallet/cashOut',
							userId: this.UserInfo.userId,
							coin: this.CashOutAmount,
							token: token2,
							authStr: tmpToken2,
							loading: true // 默认发起请求会有一个全局的Loading，设置false可去掉
						});
						if(result2.code == 200){
							this.doWxChange()
						}else{
							this.showMSG('none',result2.msg)
							flag = true
						}
					}else{
						this.showMSG('none','获取token出错啦')
						flag = true
					}
				}
			}else{
				this.showMSG('error','提现失败')
				flag = true
			}
		}else{
			this.showMSG('none','获取token出错啦')
			flag = true
		}

		if(flag){
			setTimeout(() => {
				this.KF_show = true
			}, 2000)
		}

	},

	/**
	 * 提现到零钱
	 */
	async doWxChange(){

		if(parseFloat(this.MyWealth.Money) < parseFloat(this.CashOutAmount) || !this.isNotNull(this.CashOutAmount)){//可用余额是否大于等于提现金额
			if(!this.isNotNull(this.CashOutAmount)){
				this.showMSG('none','请选择提现金额')
			}else{
				this.showMSG('none','提现金额不能超过可用余额噢!')
			}
		}else{
			let obj = {};
			obj.userid = this.UserInfo.userId;
			obj.openid = this.UserInfo.openid;
			obj.tradeDesc =  this.UserInfo.nickName + "从平台钱包提现" + this.CashOutAmount + "元";//支付详情说明
			let pay_price = this.CashOutAmount * 100
			obj.payPrice = pay_price.toString();//支付金额，单位分

			// 调用微信支付接口鉴权，返回鉴权信息
			let authinfo = await this.geAuthInfo(obj);
			console.info("1.authinfo=", authinfo);
			if(authinfo.code != 200){
				console.log("鉴权失败：" + authinfo);
				this.showMSG('error','提现失败')
				setTimeout(() => {
					this.KF_show = true
				}, 2000)
				return;
			}

			// 调用 小程序支付到零钱接口，这里是以url判断是哪个接口的
			let url = "/api/paychange";
			let respJson = await this.postWxpayServer(url, obj, authinfo);
			console.info("2.respJson=", respJson);
			if(respJson.code != 200){
				this.showMSG('error','提现失败')
				console.log("调用小程序支付到零钱接口失败：" + respJson.msg);
				setTimeout(() => {
					this.KF_show = true
				}, 2000)
				return;
			}else{
				this.TX_show = true
				setTimeout(() => {
					this.initMyWealth()
				}, 1000)
			}
		}
	},

	// 长按保存图片
	saveImg(e){
	  let url = this.customerServiceUrl
	  //用户需要授权
	  wx.getSetting({
		success: (res) => {
			if (!res.authSetting['scope.writePhotosAlbum']) {
			 wx.authorize({
			  scope: 'scope.writePhotosAlbum',
			  success:()=> {
			   // 同意授权
			   this.saveImg1(url);
			  },
			  fail: (res) =>{
			   console.log(res);
			  }
			 })
			}else{
			 // 已经授权了
			 this.saveImg1(url);
			}
		   },
		   fail: (res) =>{
			console.log(res);
		   }
		})
	},

	saveImg1(url){
		wx.getImageInfo({
		   src: url,
		   success:(res)=> {
			let path = res.path;
			wx.saveImageToPhotosAlbum({
			 filePath:path,
			 success:(res)=> {
				this.showMSG("success","保存成功！")
			 },
			 fail:(res)=>{
			  console.log(res);
			 }
			})
		   },
		   fail:(res)=> {
			console.log(res);
		   }
		})
	},

	/**
	 * 点击轮播图跳转
	 */
	clickSwiper(index){
		this.routeAD(this.AdsList[index].link,this.AdsList[index].source)
	},

	/**
	 * 新收益提醒
	 */
	async incomeRemind(){
		let res = await this.$u.api.unifyMiniRest({
			systemid: "payment",
			url: "/points/token",
			userId: this.UserInfo.userId,
			loading: false
		});
		if(res.code == 200 && this.isNotNull(res.data)){
			let token = res.data
			let tmpToken = this.sault_md5_token(token)
			let result = await this.$u.api.unifyMiniRest({
				systemid: "payment",
				url: "/dialog/get",
				userId: this.UserInfo.userId,
				token: token,
				authStr: tmpToken
			});
			if(result.code == 200){
				if(result.isPopup){
					this.income_show = true
					this.updateIncomeRemind()
				}
			}
		}
	},

	/**
	 * 更新收益弹窗数据
	 */
	async updateIncomeRemind() {
		let res = await this.$u.api.unifyMiniRest({
			systemid: "payment",
			url: "/points/token",
			userId: this.UserInfo.userId,
			loading: false
		});
		if(res.code == 200 && this.isNotNull(res.data)){
			let token = res.data
			let tmpToken = this.sault_md5_token(token)
			await this.$u.api.unifyMiniRest({
				systemid: "payment",
				url: "/dialog/update",
				userId: this.UserInfo.userId,
				token: token,
				authStr: tmpToken
			});
		}
	}

  },
};
</script>

<style scoped>
*{margin: 0;padding: 0;border: 0;list-style: none;text-decoration: none;color: inherit;font-weight: normal;font-family: "微软雅黑";box-sizing: border-box;font-style: normal;}
.body{width: 100%;overflow-x: hidden;background: url(https://7072-prodcloud-0goj9rn820c29ef9-1305572243.tcb.qcloud.la/icon/PersonalIndex/bg.jpg?sign=0774ff2496394fe1546f35ebf013bde8&t=1642562104) top center no-repeat #FFFFFF;background-size: 100%;}
img{vertical-align: middle;max-width: 100%;}
a:hover,a:active,a:visited,a:link,a:focus{-webkit-tap-highlight-color: transparent;outline:none;text-decoration: none;
}
.bg-blue{background: #0068ff;}
.text-blue{color: #289ef4!important;}
.text-yellow{color: #ffa33e!important;}
.btn{color: #FFFFFF;height: 80rpx;line-height: 80rpx;text-align: center;font-size: 30rpx;border-radius: 5rpx;outline: none;}
.wfull{width: 100%;}
.top-title{position: absolute;top: 20rpx;left: 0;width: 100%;font-size: 38rpx;color: #FFFFFF;text-align: center;}

.line-progress{
	width: 440rpx;
}

.color-white{
	color: #FFFFFF;
}

.icon-line{
	display: flex;
	justify-content: space-between;
}

.space-between{
	justify-content: space-between;
}

.code{
	width: 175rpx;
	overflow: hidden;
    text-overflow:ellipsis;
    white-space: nowrap;
}

.u-box {
    padding: 20rpx 30rpx 0 30rpx;
}

.u-page{
	padding-bottom: 0rpx !important;
}

.u-flex-column{
	display: flex;
	flex-direction: column;
	align-items: center;
}

.module-box {
	width: 95%;
	margin: 0 auto;
	background-color: #fffdfe;
	border-radius: 20rpx;
	margin-top: 20rpx;
	margin-bottom: 20rpx;
}

.module-box-title {
	width: 93%;
	margin: 0 auto;
	font-size: 34rpx;
	font-weight: bold;
	height: 100rpx;
	line-height: 100rpx;
	border-bottom: 1rpx #e5e5e5 solid;
	padding-left: 20rpx;
	padding-right: 20rpx;
}

.height300 {
	height: 300rpx;
}

.height270 {
	height: 270rpx;
}

.functions-item {
	width: 25%;
	display: flex;
	flex-direction: column;
	justify-content: center;
	align-items: center;
}

.applications-item {
	width: 25%;
	display: flex;
	flex-direction: column;
	justify-content: center;
	align-items: center;
}

.color-black {
	color: #000000;
}

.fw-545 {
	font-weight: 545;
}

/* 完善用户头像和昵称 */
.aui-flex-user {
	width: 140rpx;
	height: 140rpx;
	margin-right: 15px;
	position: relative;
	border: 0;
	border: none;
	outline: none;
	margin: 0;
	padding: 0;
	overflow: visible;
	border-radius: 100%;
	padding-left: 0;
	padding-right: 0;
	line-height: 0;
}

.aui-flex-user::after {
	border: none;
}

.aui-flex-vip {
	width: 50rpx;
	height: 50rpx;
	display: block;
	position: absolute;
	right: -8rpx;
	top: 100rpx;
}

.aui-flex-user img {
	width: 140rpx;
	height: 140rpx;
	display: block;
	border: 1px solid #fff;
	border-radius: 100%;
}

.aui-flex-gradual {
	background-image: -webkit-gradient(linear,left top,right top,from(#fe941f),to(#ffbf56));
	background-image: -webkit-linear-gradient(left,#fe941f,#ffbf56);
	background-image: -moz-linear-gradient(left,#fe941f,#ffbf56);
	background-image: linear-gradient(to right,#fe941f,#ffbf56);
	background-color: #ffbf56;
	padding: 40px 15px;
}

.aui-flex-vip img {
	width: 100%;
	height: auto;
	display: block;
	border: none;
	border-radius: 0;
}

.user-head-bg {
	background: url("/static/images/improveinfo.png") no-repeat left top / 100%;
}
/* 完善用户头像和昵称end */

.slot-content2 {
	font-size: 28rpx;
	text-align: left;
	width: 93%;
	margin: 0 auto;
	padding-top: 20rpx;
	padding-bottom: 20rpx;
}

/* 区域服务商 */
.provider-item {
	width: 95%;
	height: 110rpx;
	margin: 0 auto;
	background-image: url(https://dev-1308272144.cos.ap-guangzhou.myqcloud.com/Meily/icon/personalIndex5.png);background-size: 100% 100%;
	display: flex;
}

.provider-item-center {
	width: 60%;
	height: 100%;
	display: flex;
	align-items: center;
}

.provider-center-title {
	width: 400rpx;
	color: #ffffff;
	white-space: nowrap;
	overflow: hidden;
	text-overflow: ellipsis;
}

.provider-item-right {
	width: 23%;
	height: 100%;
	display: flex;
	align-items: center;
}

.provider-item-right-btn {
	background-color: #ffffff;
	color:rgb(34, 31, 73);
	padding: 5rpx 10rpx;
	border-radius: 10rpx;
}

.application-bar {
	width: 95%;
	margin: 0 auto;
	border-radius: 15rpx;
	display: flex;
	background-color: #fffdfe;
}

.application-bar-item {
	padding: 30rpx 20rpx;
	display: flex;
	flex-direction: column;
	justify-content: center;
	align-items: center;
	width: 25%;
}

.pane {
	border-top: 0;
	min-height: 100px;
	display: none;
}

.dp-number-current {
	padding: 14rpx 45rpx;
}

.dp-number-current ol li {
	width: 130rpx;
	color: #403d3e;
	text-align: center;
	float: left;
	display: inline-block;
	border: 1px solid #403d3e;
	border-radius: 5px;
	margin: 1% 1.2%;
	font-size: 26rpx;
	height: 65rpx;
	line-height: 60rpx;
	position: relative;
}

.dp-pane-text {
	font-size: 14px;
	color: #333333;
	padding: 20px 15px 0 15px;
}

.dp-pane-text span {
	color: #999999;
	float: right;
}

.dp-number-inp {
	padding: 10px 15px;
	font-size: 14px;
}

.dp-number-inp .dp-line {
	border-radius: 5px;
	border: 1px solid #dbdbdb;
	margin-right: 10px;
}

.dp-number-inp div {
	float: left;
	height: 30px;
	line-height: 30px;
}

.dp-number-inp input {
	height: 30px;
	line-height: 30px;
	border: none;
	padding-left: 5px;
	background: none;
}

.dp-number-inp label em {
	color: #d5a97b;
}

.tabPanel .hit {
	background: #fff;
	color: #d5a97b;
	position: relative;
	border: 1px solid #d5a97b;
}

.dp-number-current ol .current {
	/*
	background-image: url('');
	*/
    background-image: url(https://www-1308272144.cos.ap-guangzhou.myqcloud.com/Meily/icon/incomeIndex/income-cash-seleted.png);
	background-position: right bottom;
	background-repeat: no-repeat;
	background-size: 25px;
	display: block;
	float: left;
}

.cash-out-btn{
	width: 100%;
	text-align: center;
}

.income-box {
	width: 95%;
	height: 30;
	margin: 0 auto;
	border-radius: 30rpx;
}

.income-money {
	text-align: center;
	display: flex;
	flex-direction: column;
	justify-content: center;
}

.income-money-text {
	color: #403d3e;
	font-size: 26rpx;
}

.income-tips {
	color: #403d3e;
	margin: 0 auto;
	font-size: 22rpx;
	text-align: center;
}

.income-tips span {
	text-decoration: underline;
	color: #eb5468;
}

.fz-30 {
	font-size: 60rpx;
}

.slot-wrap {
  display: flex;
  align-items: center;
  /* 如果您想让slot内容占满整个导航栏的宽度 */
  flex: 1;
  /* 如果您想让slot内容与导航栏左右有空隙 */
  padding: 0 30rpx;
}

.slot-content3 {
	font-size: 28rpx;
	text-align: center;
	width: 93%;
	margin: 0 auto;
	padding-top: 50rpx;
	padding-bottom: 50rpx;
}

</style>
